package vg.com.chatonline.adapter;

import java.util.ArrayList;

import com.woozzu.android.util.StringMatcher;

import vg.com.chatonline.activity.R;
import vg.com.classdata.message_friend;
import vg.com.classdata.yahoo;
import vg.com.frame.friend_frame;
import vg.com.frame.message_friend_frame;
import vg.com.frame.yahoo_frame;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class adapter_friend_message extends BaseAdapter implements SectionIndexer {

	private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private ImageView icon;
	private Button bt_name;
	private TextView tv_status;
	
	private Context mContext;

	//private ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String,String>>();
	private static LayoutInflater inflater = null;

	// Friend m_Friend=new Friend(status, name, url_img)

	private ArrayList<message_friend> mItems=new ArrayList<message_friend>();
		 
		public adapter_friend_message(Context context,ArrayList<message_friend> items) {
		     this.mContext = context;
		     this.mItems = items;
		     inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

	public int getCount() {
		// TODO Auto-generated method stub
		return mItems.size();
	}

	public message_friend getItem(int position) {
		return mItems.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
	
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_message_friend, null);
		}
		View v=inflater.inflate(R.layout.message_layout, null);
		TextView tv_message=(TextView)v.findViewById(R.id.tv_message_friend);
		TextView tv_time=(TextView)v.findViewById(R.id.tv_message_time);
		tv_message.setText(mItems.get(position).getMessage());
		tv_time.setText(mItems.get(position).getTime());
		//String message=mItems.get(position).getMessage();
		//String time=mItems.get(position).getTime();
		
		message_friend_frame post=(message_friend_frame)convertView.findViewById(R.id.frame_message_friend);
		post.fillData(mItems.get(position).getName(),mItems.get(position).getUrl_img(),mItems.get(position).getIconRes(),v);

	//	bt_name = (Button) vi.findViewById(R.id.contact_tv_name_1);
	//	tv_status = (TextView) vi
		//		.findViewById(R.id.contact_tv_status_1);
		
	//	 icon = (ImageView) vi.findViewById(R.id.contact_img_user_1);
//		 if(mItems.get(position).getImage() != null)
//		 {
//			icon.setImageBitmap(mItems.get(position).getImage());
//		 }
//		 else
//		 {
//			 icon.setImageResource(R.drawable.ic_launcher);
//			 //getItem(position).
//			//icon.setImageBitmap(mItems.get(position).getImage());
////			/ mItems.get(position).loadImage(mItems.get(position).getUrl_img());
//		 }
//		 bt_name.setText(mItems.get(position).getName());
//		 tv_status.setText(mItems.get(position).getStatus());
//		 String tv=tv_status.getText().toString();
//		 String bt=bt_name.getText().toString();
//		 Log.i("adapter1", tv);
//		 Log.i("adapter2", bt);
		return convertView;

	}

	public int getPositionForSection(int section) {
		// TODO Auto-generated method stub
		for (int i = section; i >= 0; i--) {
			for (int j = 0; j < getCount(); j++) {
				if (i == 0) {
					// For numeric section
					for (int k = 0; k <= 9; k++) {
						if (StringMatcher.match(String.valueOf(getItem(j).getName().charAt(0)), String.valueOf(k)))
							return j;
					}
				} else {
					if (StringMatcher.match(String.valueOf(getItem(j).getName().charAt(0)), String.valueOf(mSections.charAt(i))))
						return j;
				}
			}
		}
		return 0;
	}

	public int getSectionForPosition(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public Object[] getSections() {
		// TODO Auto-generated method stub
		String[] sections = new String[mSections.length()];
		for (int i = 0; i < mSections.length(); i++)
			sections[i] = String.valueOf(mSections.charAt(i));
		return sections;
	}

	

}
